<?php
/**
 * 众安饮食合理评估
 * Created by PhpStorm.
 * User: Derek
 * Date: 2016/12/12
 * Time: 16:41
 */

namespace App\Services\Result;

use App\Interf\Resultable;
use Monolog\Logger;
use TestingConfig;
use ParamConfig;
use App\Models\LogHelper;

class ZADietResult extends ZAhealthVtwoResult implements Resultable
{
//    private $answers;

    private $EvaluationAnalysisObj;

    private $DietRecommendationObj;


    public function __construct($answersStr = null)
    {
        $this->answers = $this->convertAnswer($answersStr);
        $this->EvaluationAnalysisObj = $this->configAttrObj(1, '评估分析');
        $this->DietRecommendationObj = $this->configAttrObj(2, '饮食推荐');
    }

    public function result()
    {
        $attrs = [];

        $attrs[] = $this->getEvaluationAnalysisObj();

        $attrs[] = $this->getDietRecommendationObj();


        $data['attrs'] = $attrs;
        $data['chatparam'] = $this->getChatparam();
        //图类型常规图
        $data['chatType'] = $this->getChatType();

        return $data;
    }

    protected function getEvaluationAnalysisObj()
    {
        $note = $this->getNoteByTag('tag.69.testing.analyze');

        $obj = $this->EvaluationAnalysisObj;
        $obj = $this->compactAttrObj($obj['attr_id'], $obj['title'], $note);
        LogHelper::log($obj);
        return $obj;
    }

    protected function getDietRecommendationObj()
    {
        $recipe['banner_title'] = null;
        $recipe['banner_pic'] = "";
        $recipeData = $this->getRecipedetail();
        $recipe['recipe'] = $recipeData;
        $obj = $this->DietRecommendationObj;
        $obj = $this->compactAttrObj($obj['attr_id'], $obj['title'], $recipe);
        return $obj;
    }

    protected function getNoteByTag($tag)
    {
        $values = ParamConfig::getByTag($tag);

        foreach ($values as $value) {
            $exp = $value['key'];
            while (preg_match($regExp = '/@\(?(\d+)\)?/', $exp, $matches)) {
                $v = $this->getAnswerObjByIndex($this->answers, $matches[1]);
                $exp = preg_replace($regExp, $v['weight'], $exp, 1);
            }
            $hit = eval("return $exp;");
            if ($hit) {
                return $value['value'];
            }
        }
    }

    /**
     * 图标对应的BMI数据
     */
    protected function getChatparam()
    {
        $answer = $this->getAnswerObjByIndex($this->answers, 31);

        return $answer['weight'];
    }

    protected function compactAttrObj($id, $title, $note, $extra = '')
    {
        return [
            'attr_id' => $id,
            'title' => $title,
            'note' => $note,
            'extra' => $extra
        ];
    }

    /**将answer转换为数组
     * @param $answerStr
     * @return array
     */
    protected function convertAnswer($answerStr)
    {
        LogHelper::log('FINAL ANSWER STR============>');
        LogHelper::log($answerStr);
        $answers = [];
        $answerArr = explode('##', $answerStr);
        if (count($answerArr) > 0) {
            foreach ($answerArr as $answer) {
                $singleArr = explode(':', $answer);
                if (count($singleArr) > 0) {
                    $obj['index'] = $singleArr[0];
                    $obj['weight'] = $singleArr[1];
                    $obj['answer'] = $singleArr[2];
                    array_push($answers, $obj);
                }
            }
        }
        return $answers;
    }

    /**返回食谱
     * @return array|string
     */
    protected function getRecipe()
    {
        $recipeList = ParamConfig::getByTag('tag.69.testing.recipe');
        $m = date('m');
        if ($m >= 3 && $m <= 5) {
            // 春季
            foreach ($recipeList as $item) {
                if ($item['key'] == 'za.diet.recipe.s1') {
                    $recipe = $item['value'];
                    break;
                }
            }
        } elseif ($m >= 6 && $m <= 8) {
            // 夏季
            foreach ($recipeList as $item) {
                if ($item['key'] == 'za.diet.recipe.s2') {
                    $recipe = $item['value'];
                    break;
                }
            }
        } elseif ($m >= 9 && $m <= 11) {
            // 秋季
            foreach ($recipeList as $item) {
                if ($item['key'] == 'za.diet.recipe.s3') {
                    $recipe = $item['value'];
                    break;
                }
            }
        } else {
            // 冬季
            foreach ($recipeList as $item) {
                if ($item['key'] == 'za.diet.recipe.s4') {
                    $recipe = $item['value'];
                    break;
                }
            }
        }

        return $recipe;
    }
}